package cn.yunhe.dao.impl;

import cn.yunhe.BaseDB;
import cn.yunhe.dao.IHuiFuDao;
import cn.yunhe.entity.Admin;
import cn.yunhe.entity.HuiFu;
import cn.yunhe.entity.LiuYan;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class HuiFuDao extends BaseDB implements IHuiFuDao {
    Connection conn = null;
    PreparedStatement psmt = null;
    ResultSet rs = null;

    @Override//添加回复
    public boolean addHuiFu(String context, int lid, int uid) {
        String sql = "insert into tb_huifu(hui_context,liu_id,c_id,createdate) values(?,?,?,now())";
        Object[] arr = {context, lid, uid};
        return updateSql2(sql, arr) > 0;
    }

    @Override//返回一条留言的所有回复
    public List<HuiFu> getAAllHuiFu(int lid) {
        List<HuiFu> huiFuList=new ArrayList<>();
        try {
            String sql = "select * from tb_huifu h,tb_admin a where h.c_id=a.c_id and h.liu_id=? order by h.createdate desc";
            conn = getConn();
            psmt = conn.prepareStatement(sql);
            psmt.setInt(1, lid);
            rs = psmt.executeQuery();
            while (rs.next()){
                HuiFu huiFu =new HuiFu(rs.getString("hui_context"),new Admin(rs.getInt("c_id"),rs.getString("c_name")),
                        rs.getDate("createdate"));
                huiFuList.add(huiFu);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(conn,psmt,rs);
        }
        return huiFuList;
    }

    @Override
    public List<HuiFu> getAllHuiFu() {
        List<HuiFu> huiFuList=new ArrayList<>();
        try {
            String sql = "select * from tb_huifu h,tb_admin a where h.c_id=a.c_id order by h.createdate desc";
            conn = getConn();
            psmt = conn.prepareStatement(sql);
            rs = psmt.executeQuery();
            while (rs.next()){
                HuiFu huiFu =new HuiFu(rs.getString("hui_context"),new LiuYan(rs.getInt("liu_id")),new Admin(rs.getInt("c_id"),rs.getString("c_name")),
                        rs.getDate("createdate"));
                huiFuList.add(huiFu);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(conn,psmt,rs);
        }
        return huiFuList;
    }
}
